package com.cat.model.biz.service.api.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.biz.service.api.ModelService;
import com.cat.model.common.model.common.CompanyInfoVO;
import com.cat.model.common.model.common.ModelExperVO;
import com.cat.model.common.model.common.ModelInfoVO;
import com.cat.model.common.model.common.ModelScalePictureVO;
import com.cat.model.common.model.common.ModelStyleVO;
import com.cat.model.common.model.common.ModelZedCardVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.common.util.PageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.bean.ModelInfoBean;
import com.cat.model.web.bean.ModelInfoSearchBean;

@Service("modelService")
public class ModelServiceImpl implements ModelService{
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Override
	public String modelList(ModelInfoSearchBean model, int page, int size){
		List<ModelInfoBean> modelList = plmCommDAO.getModelInfo(model);
		
		List<ModelInfoBean> beanList = new ArrayList<ModelInfoBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(modelList == null || modelList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(ModelInfoBean vo : modelList){			
			ModelInfoBean bean = new ModelInfoBean();
			bean.setId(vo.getId());
			bean.setName(vo.getName());
			bean.setCoverImage(ImageUtil.wrapImageUrl(vo.getCoverImage()));
			bean.setAge(vo.getAge());
			bean.setSex(vo.getSex());
			bean.setCityCode(vo.getCityCode());
			bean.setCountryCode(vo.getCountryCode());
			bean.setPrice(vo.getPrice());
			
			beanList.add(bean);
		}
		
		int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<ModelInfoBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
	@Override
	public String modelInfo(long modelId) {
		ModelInfoVO modelInfo = modelMapper.getModelInfo(modelId);
		if(null == modelInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("coverImage", ImageUtil.wrapImageUrl(modelInfo.getCoverImage()));
			infoJson.put("modelId", modelInfo.getId());
			infoJson.put("modelName", modelInfo.getName());
			infoJson.put("price", modelInfo.getPrice());
			infoJson.put("modelCode", modelInfo.getModelCode());
			infoJson.put("sex", modelInfo.getSex());
			infoJson.put("age", modelInfo.getAge());
			infoJson.put("countryCode", modelInfo.getCountryCode());
			infoJson.put("provinceCode", modelInfo.getProvinceCode());
			infoJson.put("cityCode", modelInfo.getCityCode());
			infoJson.put("height", modelInfo.getHeight());
			infoJson.put("weight", modelInfo.getWeight());
			infoJson.put("shoeSize", modelInfo.getShoeSize());
			infoJson.put("bustSize", modelInfo.getBustSize());
			infoJson.put("hiplineSize", modelInfo.getHiplineSize());
			infoJson.put("waistSize", modelInfo.getWaistSize());
			infoJson.put("cupSize", modelInfo.getCupSize());
			String companyName = null;
			if(modelInfo.getCompanyId()!=null){
				CompanyInfoVO companyInfoVO = modelMapper.getCompanyInfo(modelInfo.getCompanyId());
				if(companyInfoVO!=null){
					companyName = companyInfoVO.getName();
				}
			}
			infoJson.put("companyName", companyName);
			infoJson.put("richResume", modelInfo.getRichResume());
			infoJson.put("hairColorCode", modelInfo.getHairColorCode());
			infoJson.put("raceCode", modelInfo.getRaceCode());
			infoJson.put("eyeCode", modelInfo.getEyeCode());
			infoJson.put("armLength", modelInfo.getArmLength());
			infoJson.put("upArmSize", modelInfo.getUpArmSize());
			infoJson.put("legSize", modelInfo.getLegSize());
			infoJson.put("thighSize", modelInfo.getThighSize());
			infoJson.put("skinColorCode", modelInfo.getSkinColorCode());
			infoJson.put("ageGroup", modelInfo.getAgeGroup());
			infoJson.put("neckLength", modelInfo.getNeckLength());
			infoJson.put("neckSize", modelInfo.getNeckSize());
			infoJson.put("remark", modelInfo.getRemark());
			infoJson.put("headSize", modelInfo.getHeadSize());
			infoJson.put("collarSize", modelInfo.getCollarSize());
			infoJson.put("shoulderSize", modelInfo.getShoulderSize());
			infoJson.put("annLength", modelInfo.getAnnLength());
			infoJson.put("sleeveLength", modelInfo.getSleeveLength());
			infoJson.put("centerBack", modelInfo.getCenterBack());
			infoJson.put("chestSize", modelInfo.getChestSize());
			infoJson.put("underbustSize", modelInfo.getUnderbustSize());
			infoJson.put("highHipSize", modelInfo.getHighHipSize());
			infoJson.put("midHipSize", modelInfo.getMidHipSize());
			infoJson.put("lowHipSize", modelInfo.getLowHipSize());
			infoJson.put("wristSize", modelInfo.getWristSize());
			infoJson.put("outseamSize", modelInfo.getOutseamSize());
			infoJson.put("inseamSize", modelInfo.getInseamSize());
			infoJson.put("calfSize", modelInfo.getCalfSize());
			infoJson.put("ankleSize", modelInfo.getAnkleSize());
			infoJson.put("suitSizes", modelInfo.getSuitSizes());
			
			List<ModelStyleVO> styles = modelMapper.getModelStyleList(modelInfo.getId());
			JSONArray styleArray = new JSONArray();
			if(styles!=null&&styles.size()>0){
				for(ModelStyleVO style : styles){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("styleCode", style.getStyleCode());
					
					styleArray.put(detailJson);
				}
			}
			infoJson.put("styleList", styleArray);
			
			List<ModelExperVO> expers = modelMapper.getModelExperList(modelInfo.getId());
			JSONArray experArray = new JSONArray();
			if(expers!=null&&expers.size()>0){
				for(ModelExperVO exper : expers){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("experCode", exper.getExperCode());
					
					experArray.put(detailJson);
				}
			}
			infoJson.put("experList", experArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String modelScalePicture(long modelId) {
		ModelInfoVO modelInfo = modelMapper.getModelInfo(modelId);
		if(null == modelInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<ModelScalePictureVO> pictures = modelMapper.getModelScalePictureList(modelInfo.getId());
			JSONArray pictureArray = new JSONArray();
			if(pictures!=null&&pictures.size()>0){
				for(ModelScalePictureVO picture : pictures){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("modelId", picture.getModelId());
					detailJson.put("mateType", picture.getMateType());
					detailJson.put("image", picture.getImage());
					detailJson.put("vedio", picture.getVedio());
					
					pictureArray.put(detailJson);
				}
			}
			infoJson.put("pictureList", pictureArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String modelZedCard(long modelId) {
		ModelInfoVO modelInfo = modelMapper.getModelInfo(modelId);
		if(null == modelInfo){
			return ErrorCode.OK.response();
		}
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			List<ModelZedCardVO> zedCards = modelMapper.getModelZedCardList(modelInfo.getId());
			JSONArray zedCardArray = new JSONArray();
			if(zedCards!=null&&zedCards.size()>0){
				for(ModelZedCardVO zedCard : zedCards){				
					JSONObject detailJson = new JSONObject();
					detailJson.put("modelId", zedCard.getModelId());
					detailJson.put("image", zedCard.getImage());
					
					zedCardArray.put(detailJson);
				}
			}
			infoJson.put("zedCardList", zedCardArray);
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
}
